"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _vue = require("vue");
var _xeUtils = _interopRequireDefault(require("xe-utils"));
var _ui = require("../../ui");
var _dom = require("../../ui/src/dom");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _default = exports.default = (0, _vue.defineComponent)({
  name: 'VxeNoticeBar',
  props: {
    duration: [String, Number],
    direction: {
      type: String,
      default: () => (0, _ui.getConfig)().noticeBar.direction
    },
    speed: {
      type: String,
      default: () => (0, _ui.getConfig)().noticeBar.speed
    },
    content: String,
    vertical: Boolean,
    loop: {
      type: Boolean
    },
    size: {
      type: String,
      default: () => (0, _ui.getConfig)().noticeBar.size || (0, _ui.getConfig)().size
    }
  },
  emits: [],
  setup(props, context) {
    const {
      slots,
      emit
    } = context;
    const xID = _xeUtils.default.uniqueId();
    const {
      computeSize
    } = (0, _ui.useSize)(props);
    const refElem = (0, _vue.ref)();
    const refContentElem = (0, _vue.ref)();
    const reactData = (0, _vue.reactive)({
      animationDuration: 0
    });
    const refMaps = {
      refElem
    };
    const computeNoticeText = (0, _vue.computed)(() => {
      const {
        content
      } = props;
      return `${content || ''}`;
    });
    const computeMaps = {};
    const $xeNoticeBar = {
      xID,
      props,
      context,
      reactData,
      getRefMaps: () => refMaps,
      getComputeMaps: () => computeMaps
    };
    const dispatchEvent = (type, params, evnt) => {
      emit(type, (0, _ui.createEvent)(evnt, {
        $noticeBar: $xeNoticeBar
      }, params));
    };
    const noticeBarMethods = {
      dispatchEvent
    };
    const noticeBarPrivateMethods = {};
    const updateAnimationStyle = () => {
      const {
        speed
      } = props;
      const contEl = refContentElem.value;
      if (contEl) {
        let sRate = 46;
        if (speed === 'fast') {
          sRate = 118;
        } else if (speed === 'slow') {
          sRate = 18;
        }
        reactData.animationDuration = Math.ceil(contEl.scrollWidth / sRate);
      }
    };
    Object.assign($xeNoticeBar, noticeBarMethods, noticeBarPrivateMethods);
    const renderVN = () => {
      const {
        vertical,
        duration,
        direction
      } = props;
      const {
        animationDuration
      } = reactData;
      const vSize = computeSize.value;
      const noticeText = computeNoticeText.value;
      const defaultSlot = slots.default;
      const prefixSlot = slots.prefix;
      const suffixSlot = slots.suffix;
      return (0, _vue.h)('div', {
        ref: refElem,
        class: ['vxe-notice-bar', `is--${vertical ? 'vertical' : 'horizontal'}`, `dir--${direction || 'left'}`, {
          [`size--${vSize}`]: vSize
        }]
      }, [prefixSlot ? (0, _vue.h)('div', {
        class: 'vxe-notice-bar--prefix'
      }, prefixSlot({})) : (0, _ui.renderEmptyElement)($xeNoticeBar), (0, _vue.h)('div', {
        class: 'vxe-notice-bar--content'
      }, [(0, _vue.h)('div', {
        ref: refContentElem,
        class: 'vxe-notice-bar--inner'
      }, [(0, _vue.h)('div', {
        class: 'vxe-notice-bar--wrapper',
        style: {
          animationDuration: `${duration ? (0, _dom.toCssUnit)(duration, 's') : animationDuration}s`
        }
      }, defaultSlot ? defaultSlot({}) : noticeText)])]), suffixSlot ? (0, _vue.h)('div', {
        class: 'vxe-notice-bar--suffix'
      }, suffixSlot({})) : (0, _ui.renderEmptyElement)($xeNoticeBar)]);
    };
    $xeNoticeBar.renderVN = renderVN;
    (0, _vue.onMounted)(() => {
      _ui.globalEvents.on($xeNoticeBar, 'resize', updateAnimationStyle);
      updateAnimationStyle();
    });
    (0, _vue.onBeforeUnmount)(() => {
      _ui.globalEvents.off($xeNoticeBar, 'resize');
    });
    return $xeNoticeBar;
  },
  render() {
    return this.renderVN();
  }
});